import { _decorator, Component, instantiate, Node, Vec3, tween, Animation, ProgressBar } from 'cc';
import { AudioManager } from './AudioManager';
import { UIGame } from '../gui/UIGame';
import { UIManager } from '../gui/UIManager';
const { ccclass, property } = _decorator;

@ccclass('EffectManager')
export class EffectManager extends Component {
    public static Instance: EffectManager = null;
    @property({ type: Node, displayName: "金币特效预制体" }) coinPrefab: Node = null!;
    @property({ type: Node, displayName: "玩家炮弹爆炸特效" }) explodePrefab: Node = null!;

    onLoad() {
        EffectManager.Instance = this;
    }

    // 播放金币收集特效
    playCoinEffect(beginPos: Vec3, cb?: Function) {
        const coinEffect = instantiate(this.coinPrefab);
        coinEffect.setParent(UIManager.Instance.node);
        coinEffect.setWorldPosition(beginPos);
        AudioManager.Instance.playGoldCollect();

        // 目标坐标
        const endPos = UIGame.Instance.CoinNode.getWorldPosition().clone();
        // tween动画
        tween(coinEffect)
            .to(1, { worldPosition: endPos }, { easing: 'quadIn' })
            .call(() => {
                coinEffect.destroy();
                cb?.();
            })
            .start();
    }

    // 播放爆炸特效
    playExplodeEffect(pos: Vec3) {
        const explodeEffect = instantiate(this.explodePrefab);
        explodeEffect.setParent(this.node);
        explodeEffect.setWorldPosition(pos);
        explodeEffect.active = true;

        const anim = explodeEffect.getComponentInChildren(Animation);
        anim.play();
        anim.on(Animation.EventType.FINISHED, () => {
            explodeEffect.destroy();
        });
    }

}


